package com.zzyl.controller;


import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 角色分页 查询
     * @param pageNum
     * @param pageSize
     * @param roleDto
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<RoleVo>> rolePage(@PathVariable Integer pageNum,
                                                         @PathVariable Integer pageSize,
                                                         @RequestBody RoleDto roleDto){

       PageResponse<RoleVo> result = roleService.selectByPage(pageNum,pageSize,roleDto);
        return ResponseResult.success(result);

    }

    /**
     * 角色添加
     * @param roleDto
     * @return
     */
    @PutMapping
    public ResponseResult roleAdd(@RequestBody RoleDto roleDto){
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    /**
     * 角色修改
     */
    @PatchMapping
    public ResponseResult roleUpdate(@RequestBody RoleDto roleDto){
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    /**
     * 根据角儿查询旋转的资源数据
     * @param roleId
     * @return
     */
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<String>> findCheckedResources(@PathVariable Long roleId){
        List<String> checkedResources = roleService.findCheckedResources(roleId);
        return ResponseResult.success(checkedResources);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/{roleId}")
    public ResponseResult roleDelete(@PathVariable Long roleId){
        roleService.deleteRole(roleId);
        return ResponseResult.success();
    }

    /**
     * 角色下拉框
     * @return
     */
    @PostMapping("/init-roles")
    @ApiOperation(value = "角色下拉列表")
    public ResponseResult<List<RoleVo>>initRoles(){
        List<RoleVo> list =roleService.roleList();
        return ResponseResult.success(list);
    }



}
